package com.lyy.config;

import com.alibaba.csp.sentinel.command.handler.ModifyParamFlowRulesCommandHandler;
import com.alibaba.csp.sentinel.datasource.*;
import com.alibaba.csp.sentinel.init.InitFunc;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
import com.alibaba.csp.sentinel.transport.util.WritableDataSourceRegistry;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import lombok.SneakyThrows;
import java.io.File;
import java.util.List;

/**
 * @author Liangyaoyun
 */
@SuppressWarnings("all")
public class FilePersistenceConfig implements InitFunc{
    /** 规则存放位置 */
    private static final String RULE_DIR = "D:/joezhou/Sentinel";


    public void init() throws Exception {
        String flowRulePath = RULE_DIR + "/flow-rule.json";
        String degradeRulePath = RULE_DIR + "/degrade-rule.json";
        String systemRulePath = RULE_DIR + "/system-rule.json";
        String authorityRulePath = RULE_DIR + "/authority-rule.json";
        String paramFlowRulePath = RULE_DIR + "/param-flow-rule.json";
        this.mkdirIfNotExits(RULE_DIR);
        this.createFileIfNotExits(flowRulePath);
        this.createFileIfNotExits(degradeRulePath);
        this.createFileIfNotExits(systemRulePath);
        this.createFileIfNotExits(authorityRulePath);
        this.createFileIfNotExits(paramFlowRulePath);
        // 流控降级规则
        ReadableDataSource<String, List<FlowRule>> flowRuleRds
                = new FileRefreshableDataSource<>(flowRulePath, flowRuleListParser);
        FlowRuleManager.register2Property(flowRuleRds.getProperty());
        WritableDataSource<List<FlowRule>> flowRuleWds
                = new FileWritableDataSource<>(flowRulePath, this::encodeJson);
        WritableDataSourceRegistry.registerFlowDataSource(flowRuleWds);
        // 熔断降级规则
        ReadableDataSource<String, List<DegradeRule>> degradeRuleRds
                = new FileRefreshableDataSource<>(degradeRulePath, degradeRuleListParser);
        DegradeRuleManager.register2Property(degradeRuleRds.getProperty());
        WritableDataSource<List<DegradeRule>> degradeRuleWds
                = new FileWritableDataSource<>(degradeRulePath, this::encodeJson);
        WritableDataSourceRegistry.registerDegradeDataSource(degradeRuleWds);
        // 系统降级规则
        ReadableDataSource<String, List<SystemRule>> systemRuleRds
                = new FileRefreshableDataSource<>(systemRulePath, systemRuleListParser);
        SystemRuleManager.register2Property(systemRuleRds.getProperty());
        WritableDataSource<List<SystemRule>> systemRuleWds
                = new FileWritableDataSource<>(systemRulePath, this::encodeJson);
        WritableDataSourceRegistry.registerSystemDataSource(systemRuleWds);
        // 授权降级规则
        ReadableDataSource<String, List<AuthorityRule>> authorityRuleRds
                = new FileRefreshableDataSource<>(authorityRulePath, authorityRuleListParser);
        AuthorityRuleManager.register2Property(authorityRuleRds.getProperty());
        WritableDataSource<List<AuthorityRule>> authorityRuleWds
                = new FileWritableDataSource<>(authorityRulePath, this::encodeJson);
        WritableDataSourceRegistry.registerAuthorityDataSource(authorityRuleWds);
        // 热点降级规则
        ReadableDataSource<String, List<ParamFlowRule>> paramFlowRuleRds
                = new FileRefreshableDataSource<>(paramFlowRulePath, paramFlowRuleListParser);
        ParamFlowRuleManager.register2Property(paramFlowRuleRds.getProperty());
        WritableDataSource<List<ParamFlowRule>> paramFlowRuleWds
                = new FileWritableDataSource<>(paramFlowRulePath, this::encodeJson);
        ModifyParamFlowRulesCommandHandler.setWritableDataSource(paramFlowRuleWds);
    }

    private Converter<String, List<FlowRule>> flowRuleListParser = source -> JSON.parseObject(source,
            new TypeReference<List<FlowRule>>() {
            });
    private Converter<String, List<DegradeRule>> degradeRuleListParser = source ->
            JSON.parseObject(source, new TypeReference<List<DegradeRule>>() {
            });
    private Converter<String, List<SystemRule>> systemRuleListParser = source ->
            JSON.parseObject(source, new TypeReference<List<SystemRule>>() {
            });
    private Converter<String, List<AuthorityRule>> authorityRuleListParser = source ->
            JSON.parseObject(source, new TypeReference<List<AuthorityRule>>() {
            });
    private Converter<String, List<ParamFlowRule>> paramFlowRuleListParser = source ->
            JSON.parseObject(source, new TypeReference<List<ParamFlowRule>>() {
            });

    private void mkdirIfNotExits(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
    }

    @SneakyThrows
    private void createFileIfNotExits(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            file.createNewFile();
        }
    }

    private <T> String encodeJson(T t) {
        return JSON.toJSONString(t);
    }
}
